<template slot-scope="scope">
  <div class="box">
    <div class="top">
      <el-input v-model="title" placeholder="请输入专题标题" style="width: 16%;" clearable></el-input>
      <el-input v-model="subtitle" placeholder="请输入专题子标题" style="width: 16%;" clearable></el-input>
      <el-select v-model="value" placeholder="请选择排序">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" @click="handleSearch">查找</el-button>
      <el-button type="primary" icon="el-icon-download" @click="handleDaochu">导出</el-button>
      <p class="btn">
        <el-button type="primary" icon="el-icon-edit" @click="handleAdd">添加</el-button>
        <el-button type="danger" icon="el-icon-delete" @click="handleDels">批量删除</el-button>
      </p>
    </div>
    <div class="box-main">
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
        @selection-change="handleSelectionChange" border>
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="title" label="专题标题" min-width="140">
        </el-table-column>
        <el-table-column prop="subtitle" label="专题子标题" min-width="345">
        </el-table-column>
        <el-table-column prop="picUrl" label="图片" min-width="135">
          <template slot-scope="scope">
            <el-image :src="scope.row.picUrl" :preview-src-list="[scope.row.picUrl]"
              style="width: 40px;height: 40px;display: block;margin: 0 auto;"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="专题详情" min-width="135">
          <template slot-scope="scope">
            <el-button type="primary" @click="handlecheck(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="底价" min-width="135">
        </el-table-column>
        <el-table-column prop="readCount" label="阅读数量" min-width="135">
        </el-table-column>
        <el-table-column label="操作" min-width="170">
          <template slot-scope="scope">
            <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" @click="handleDel(scope.row.id)">删除</el-button>
            <!-- 模态框 -->
            <div>
              <el-dialog :title="title" :visible.sync="dialogVisible" width="800px" class="box2">
                <div v-html="content">
                </div>
              </el-dialog>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pancle">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="currentPage4" :page-sizes="[10, 20, 30, 40,50,100]" :page-size="20"
          layout="total, sizes, prev, pager, next, jumper" :total=total>
        </el-pagination>
      </div>
    </div>

  </div>
</template>
<script>
  import { listTopic, createTopic, updateTopic, deleteTopic, readTopic, batchDeleteTopic } from '@/api/topic'
  import * as XLSX from 'xlsx';
  export default {
    data() {
      return {
        content: '',
        dialogVisible: false,
        title: '',
        subtitle: '',
        value: 2,
        page: 1,
        limit: 20,
        sort: 'add_time',
        order: 'desc',
        total: null,
        currentPage4: 1,
        options: [{
          value: 1,
          label: '按序号排序'
        },
        {
          value: 2,
          label: '按时间排序'
        },
        {
          value: 3,
          label: '按价格排序'
        }],
        tableData: [],
        multipleSelection: []
      }
    },
    mounted() {
      this.handleGetList()
    },
    methods: {
      //渲染页面
      handleGetList() {
        listTopic({
          page: this.page,
          limit: this.limit,
          sort: this.sort,
          order: this.order,
          title: this.title,
          subtitle: this.subtitle
        }).then((res) => {
          console.log(res);
          this.tableData = res.data.data.list
          this.total = res.data.data.total

        })
      },
      // 搜索
      handleSearch() {
        this.handleGetList()
      },
      // 添加
      handleAdd() {
        this.$router.push({
          name: 'topicCreate',
        })
      },
      // 编辑
      handleEdit(row) {
        this.$router.push({
          name: 'topicEdit',
          query: {
            id: row.id
          }
        })
      },
      // 查看
      handlecheck(row) {
        this.dialogVisible = true
        this.title = '专题详情'
        this.content = row.content
      },
      // 批量删除
      handleDels() {
        // 获取所有选中的行 ID
        const selectedIds = this.multipleSelection.map(item => item.id);
        batchDeleteTopic({
          ids: selectedIds
        }).then((res) => {
          console.log(res);
          this.handleGetList()
        })
      },
      // 删除
      handleDel(id) {
        deleteTopic({
          id: id
        }).then((res) => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.handleGetList()
        })
      },
      // 导出
      handleDaochu() {
        this.downloadLoading = true;
        let demo = [];
        for (let i = 0; i < this.tableData.length; i++) {
          let obje = {
            专题ID: this.tableData[i].id,
            专题标题: this.tableData[i].title,
            专题子标题: this.tableData[i].subtitle,
            专题内容: this.tableData[i].content,
            专题图片: this.tableData[i].picUrl,
            商品低价: this.tableData[i].price,
            阅读量: this.tableData[i].readCount,
            专题商品: this.tableData[i].goods,
          };
          this.downloadLoading = false;
          demo.push(obje);
        }
        console.log(demo);
        const worksheet = XLSX.utils.json_to_sheet(demo); //这里面传入要导出的表格数据
        const workbook = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(workbook, worksheet, "data"); //data为自定义表格名字
        XLSX.writeFile(workbook, "专题信息.xlsx"); //one.xlsx指的是文件名
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.limit = val
        this.handleGetList()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.page = val
        this.handleGetList()
      },
      // 多选表格
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      }
    },
  }
</script>
<style lang="scss" scoped>
  .box::v-deep {
    .top {
      padding: 20px 20px 0 20px;

      .btn {
        text-align: right;
      }
    }

    .box-main {
      padding: 0 20px 20px 20px;

      th {
        text-align: center;
      }

      td {
        text-align: center;
      }

      .pancle {
        padding: 32px 16px;
        margin-top: 30px;
      }
    }

    .box2 {
      .el-dialog__header {
        text-align: center !important;
      }
    }
  }
</style>